package cn.laike.modules.dev.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.laike.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.laike.modules.dev.entity.BannerEntity;
import cn.laike.modules.dev.service.BannerService;
import cn.laike.modules.dev.utils.DevUtils;
import cn.laike.modules.sys.shiro.ShiroUtils;
import cn.laike.common.exception.RRException;
import cn.laike.common.utils.FileUtils;
import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;



/**
 * 轮播图
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-03-08 14:34:16
 */
@RestController
@RequestMapping("dev/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:banner:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bannerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:banner:info")
    public R info(@PathVariable("id") Integer id){
        BannerEntity banner = bannerService.selectById(id);

        return R.ok().put("banner", banner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:banner:save")
    public R save(@RequestBody BannerEntity banner){
    	banner.setUid(ShiroUtils.getUserId());
    	banner.setCreateTime(new Date());
        bannerService.insert(banner);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:banner:update")
    public R update(@RequestBody BannerEntity banner){
        ValidatorUtils.validateEntity(banner);
        bannerService.updateAllColumnById(banner);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:banner:delete")
    public R delete(@RequestBody Integer[] ids){
        bannerService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/upload")
	public R upload(@RequestParam("picfile") MultipartFile picfile,HttpServletRequest request) throws Exception {
		if (picfile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String url = "";
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String picFilePath = DevUtils.getBannerResourceSavePath()+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "banner"+ShiroUtils.getUserId() + "_"+System.currentTimeMillis()+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picfile.transferTo(file);
				url = file.getName();
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
		}
		return R.ok().put("url", url);
	}
}
